<?php
namespace Admin\Controller;

use Think\Controller;
class CommonController extends Controller{
    function __construct(){
        parent::__construct();
        $this->check_login();
    }
    function check_login()
    {
       if(isset($_SESSION['user'])){
           $this->assign("user",$_SESSION['user']);
           return;
       }
        //检查token是否存在，不存在就是没有登录
        if(!isset($_COOKIE['token'])){
            $this->error("您还没有登录",U('User/login'));
        }
        //检查token是否正确，如果不正确，说明是用户伪造
        $data['token']=$_COOKIE['token'];
        $User =M("User");
        $user = $User->where($data)->find();

        if(!$user){
            $this->error("非法登录",U("User/login"));
        }
        $_SESSION['user'] = $user;
        $this->assign("user", $user);
        
    }

}